package com.lab.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lab.common.DailyPageResult;
import com.lab.dto.DailyPageQueryDTO;
import com.lab.dto.DailyProgressContentDTO;
import com.lab.dto.DailyProgressDTO;
import com.lab.entity.MemberDailyProgress;
import com.lab.mapper.DailyMemberProgressMapper;
import com.lab.service.DailyMemberProgressService;
import com.lab.vo.DailyProgressVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lab.entity.DailyProgressContent;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Slf4j
public class DailyMemberProgressServiceImpl implements DailyMemberProgressService {

    @Autowired
    private DailyMemberProgressMapper dailyMemberProgressMapper;

    @Override
    public DailyPageResult pageQuery(DailyPageQueryDTO dailyPageQueryDTO) {
        // 调用对象的getPage和getPageSize方法，获取页码和每页大小
        PageHelper.startPage(dailyPageQueryDTO.getPage(), dailyPageQueryDTO.getPageSize());

        // 调用mapper的pageQuery方法，传入查询DTO
        Page<DailyProgressContent> page = dailyMemberProgressMapper.pageQuery(dailyPageQueryDTO);

        // 获取总记录数和当前页数据列表
        long total = page.getTotal();
        List<DailyProgressContent> records = page.getResult();
        // 构造并返回PageResult
        return new DailyPageResult(total, records);
    }

    @Override
    public DailyProgressVO getById(Long id) {
        return dailyMemberProgressMapper.getById(id);
    }

    @Override
    public List<DailyProgressVO> getByRoleType(String roleType) {
        return dailyMemberProgressMapper.getByRoleType(roleType);
    }

    @Override
    public List<DailyProgressVO> getByMemberName(String memberName) {
        return dailyMemberProgressMapper.getByMemberName(memberName);
    }
    @Override
    public List<DailyProgressVO> getByProgressDate(LocalDate progressDate) {
        return dailyMemberProgressMapper.getByProgressDate(progressDate);
    }

    @Override
    @Transactional
    public void addDailyProgress(DailyProgressDTO dto) {
        // 1. 插入日进度主表（不变）
        MemberDailyProgress main = new MemberDailyProgress();
        main.setMemberId(dto.getMemberId());
        main.setProgressDate(dto.getProgressDate());
        main.setRoleType(dto.getRoleType());
        dailyMemberProgressMapper.insertMain(main); // 获取主表自增ID

        // 2. 遍历 contents 列表，插入内容表
        List<DailyProgressContentDTO> contentDTOList = dto.getContents();
        for (DailyProgressContentDTO contentDTO : contentDTOList) {
            DailyProgressContent contentEntity = new DailyProgressContent();
            contentEntity.setProgressId(main.getId()); // 关联主表ID
            // 内容日期默认与进度日期一致，或从DTO获取
            contentEntity.setContentDate(dto.getProgressDate());
            contentEntity.setContent(contentDTO.getContent()); // 取DTO中的内容
            dailyMemberProgressMapper.insertContent(contentEntity);
        }
    }

    public void deleteBatch(String username) {
            dailyMemberProgressMapper.deleteByUserName(username);

    }

    @Transactional(rollbackFor = Exception.class)  // 异常时回滚
    @Override
    public void truncateAll() {
        try {
            // 1. 临时禁用外键约束（关键：绕过主表被引用的限制）
            dailyMemberProgressMapper.disableForeignKeyChecks();

            // 2. 先清空从表（daily_progress_content）
            dailyMemberProgressMapper.truncateDailyContent();

            // 3. 再清空主表（member_daily_progress）
            dailyMemberProgressMapper.truncateMemberProgress();
        } finally {
            // 4. 无论成功失败，都必须恢复外键约束（避免影响其他操作）
            dailyMemberProgressMapper.enableForeignKeyChecks();
        }
    }
}